(function(vc) {

    vc.extends({
        propTypes: {
            callBackListener: vc.propTypes.string, //父组件名称
            callBackFunction: vc.propTypes.string //父组件监听方法
        },
        data: {
            addProductGroupInfo: {
                categoryId: '',
                productId: '',
                groupId: '',
                valueId: '',
                prodDesc: '',
                userCount: '',
                groupPrice: '',
                state: '',
                startTime: '',
                endTime: '',
                rebateFlag: '',
                rebate: 0,
                productCategorys: [],
                products: [],
                values: []
            }
        },
        _initMethod: function() {
            $that._listAddProductCategorys();
            vc.initDate('addProductGroupStartTime', function(_time) {
                $that.addProductGroupInfo.startTime = _time;
            });
            vc.initDate('addProductGroupEndTime', function(_time) {
                $that.addProductGroupInfo.endTime = _time;
            })
        },
        _initEvent: function() {
            vc.on('addProductGroup', 'openAddProductGroupModal', function() {
                $('#addProductGroupModel').modal('show');
            });
        },
        methods: {
            addProductGroupValidate() {
                return vc.validate.validate({
                    addProductGroupInfo: $that.addProductGroupInfo
                }, {
                    'addProductGroupInfo.valueId': [{
                            limit: "required",
                            param: "",
                            errInfo: "规格值不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "规格值不能超过30"
                        },
                    ],
                    'addProductGroupInfo.prodDesc': [{
                            limit: "required",
                            param: "",
                            errInfo: "团购产品简介不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "256",
                            errInfo: "团购产品简介不能超过256"
                        },
                    ],
                    'addProductGroupInfo.userCount': [{
                            limit: "required",
                            param: "",
                            errInfo: "拼团人数不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "拼团人数不能超过30"
                        },
                    ],
                    'addProductGroupInfo.groupPrice': [{
                            limit: "required",
                            param: "",
                            errInfo: "拼团金额不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "拼团金额不能超过30"
                        },
                    ],
                    'addProductGroupInfo.state': [{
                            limit: "required",
                            param: "",
                            errInfo: "状态不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "12",
                            errInfo: "状态不能超过12"
                        },
                    ],
                    'addProductGroupInfo.startTime': [{
                            limit: "required",
                            param: "",
                            errInfo: "开始时间不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "开始时间不能超过30"
                        },
                    ],
                    'addProductGroupInfo.endTime': [{
                            limit: "required",
                            param: "",
                            errInfo: "结束时间不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "结束时间不能超过30"
                        },
                    ],
                    'addProductGroupInfo.rebateFlag': [{
                            limit: "required",
                            param: "",
                            errInfo: "是否返佣不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "12",
                            errInfo: "是否返佣不能超过12"
                        },
                    ],
                    'addProductGroupInfo.rebate': [{
                            limit: "required",
                            param: "",
                            errInfo: "团长返佣不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "团长返佣不能超过30"
                        },
                    ],
                });
            },
            saveProductGroupInfo: function() {
                if (!$that.addProductGroupValidate()) {
                    vc.toast(vc.validate.errInfo);

                    return;
                }
                $that.addProductGroupInfo.shopId = vc.getCurrentCommunity().shopId;
                vc.http.apiPost(
                    '/productGroup.saveProductGroup',
                    JSON.stringify($that.addProductGroupInfo), {
                        emulateJSON: true
                    },
                    function(json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addProductGroupModel').modal('hide');
                            $that.clearAddProductGroupInfo();
                            vc.emit('productGroupManage', 'listProductGroup', {});

                            return;
                        }
                        vc.toast(_json.msg);

                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');

                        vc.toast(errInfo);

                    });
            },
            clearAddProductGroupInfo: function() {
                let _productCategorys = $that.addProductGroupInfo.productCategorys;
                let _products = $that.addProductGroupInfo.products;
                $that.addProductGroupInfo = {
                    categoryId: '',
                    productId: '',
                    valueId: '',
                    prodDesc: '',
                    userCount: '',
                    groupPrice: '',
                    state: '',
                    startTime: '',
                    endTime: '',
                    rebateFlag: '',
                    rebate: 0,
                    productCategorys: _productCategorys,
                    products: _products,
                    values: []
                };
            },
            _listAddProductCategorys: function() {
                let param = {
                    params: {
                        page: 1,
                        row: 100,
                        shopId: vc.getCurrentCommunity().shopId,

                    }
                };
                //发送get请求
                vc.http.apiGet('/productCategory.queryProductCategory',
                    param,
                    function(json, res) {
                        let _productCategoryManageInfo = JSON.parse(json);
                        $that.addProductGroupInfo.productCategorys = _productCategoryManageInfo.data;
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _changeAddProductCatalorys: function(_page, _rows) {
                let param = {
                    params: {
                        page: 1,
                        row: 100,
                        shopId: vc.getCurrentCommunity().shopId,
                        categoryId: $that.addProductGroupInfo.categoryId
                    }
                };
                //发送get请求
                vc.http.apiGet('/product.queryProduct',
                    param,
                    function(json, res) {
                        var _productManageInfo = JSON.parse(json);
                        $that.addProductGroupInfo.products = _productManageInfo.data;

                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _changeAddProduct: function() {
                // $that.addProductGroupInfo.products.forEach(_product => {
                //     if (_product.productId == $that.addProductGroupInfo.productId) {
                //         $that.addProductGroupInfo.values = _product.productSpecValues;
                //         $that.addProductGroupInfo.prodDesc = _product.prodDesc;
                //     }
                // });

                let param = {
                    params: {
                        page: 1,
                        row: 1,
                        shopId: vc.getCurrentCommunity().shopId,
                        productId: $that.addProductGroupInfo.productId
                    }
                };
                //发送get请求
                vc.http.apiGet('/product.queryProduct',
                    param,
                    function(json, res) {
                        let _productManageInfo = JSON.parse(json);
                        $that.addProductGroupInfo.values = _productManageInfo.data[0].productSpecValues;
                        $that.addProductGroupInfo.prodDesc = _productManageInfo.data[0].prodDesc;
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _getValueSpecName: function(_value) {
                let _specName = "";
                _value.productSpecDetails.forEach(item => {
                    _specName += (item.detailValue + ";");
                })
                return _specName;
            }

        }
    });

})(window.vc);